package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.ApArticleHotService;
import com.heima.common.constants.HotArticleConstants;
import com.heima.model.mess.ArticleBehaviorAggsData;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * 更新热点文章行为数据的Kafka的监听器
 */
@Slf4j
@Component
public class ApArticleHotKafkaListener {

    @Autowired
    private ApArticleHotService apArticleHotService;

    @KafkaListener(topics = HotArticleConstants.HOT_ARTICLE_OUT_TOPIC)
    public void msg(ConsumerRecord<String,String> consumerRecord){

        if(consumerRecord!=null){
            log.info("[更新热点文章行为数据]开始执行");
            String json = consumerRecord.value();
            ArticleBehaviorAggsData data = JSON.parseObject(json, ArticleBehaviorAggsData.class);
            apArticleHotService.recomputeHotArticle(data);
            log.info("[更新热点文章行为数据]执行完毕");
        }
    }
}
